using System;
using System.Globalization;
using System.IO;
using System.Text;

namespace RazorGenerator
{
	/// <summary>
	/// 模板基类
	/// </summary>
	public class RazorTemplateBase
	{
		private string content;
		private readonly StringBuilder stringBuilder = new StringBuilder();

		/// <summary>
		/// Layout
		/// </summary>
		public RazorTemplateBase Layout { get; set; }

		/// <summary>
		/// Output
		/// </summary>
		public TextWriter Output => output ??= new StringWriter(stringBuilder);

		private TextWriter output;

		/// <summary>
		/// 执行
		/// </summary>
		public virtual void Execute() { }

		public void WriteLiteral(string textToAppend)
		{
			if (!string.IsNullOrEmpty(textToAppend))
			{
				stringBuilder.Append(textToAppend);
			}
		}

		public void Write(object value)
		{
			if (value != null)
			{
				WriteLiteral(Convert.ToString(value, CultureInfo.InvariantCulture));
			}
		}

		public void Write(bool value)
		{
			WriteLiteral(value.ToString());
		}

		public void Write(int value)
		{
			WriteLiteral(value.ToString(CultureInfo.InvariantCulture));
		}

		public void Write(long value)
		{
			WriteLiteral(value.ToString(CultureInfo.InvariantCulture));
		}

		public void Write(float value)
		{
			WriteLiteral(value.ToString(CultureInfo.InvariantCulture));
		}

		public void Write(double value)
		{
			WriteLiteral(value.ToString(CultureInfo.InvariantCulture));
		}

		public void Write(decimal value)
		{
			WriteLiteral(value.ToString(CultureInfo.InvariantCulture));
		}

		public string RenderBody()
		{
			return content;
		}

		public string TransformText()
		{
			Execute();
			if (Layout != null)
			{
				Layout.content = stringBuilder.ToString();
				return Layout.TransformText();
			}
			else
			{
				return stringBuilder.ToString();
			}
		}

		public void Clear()
		{
			stringBuilder.Clear();
			if (Layout != null)
			{
				Layout.content = string.Empty;
			}
		}

		public void WriteLiteralTo(TextWriter writer, string text)
		{
			writer.Write(text);
		}

		public void WriteTo(TextWriter writer, object value)
		{
			writer.Write(Convert.ToString(value, CultureInfo.InvariantCulture));
		}

		public void WriteTo(TextWriter writer, bool value)
		{
			writer.Write(value.ToString());
		}

		public void WriteTo(TextWriter writer, int value)
		{
			writer.Write(value.ToString(CultureInfo.InvariantCulture));
		}

		public void WriteTo(TextWriter writer, long value)
		{
			writer.Write(value.ToString(CultureInfo.InvariantCulture));
		}

		public void WriteTo(TextWriter writer, float value)
		{
			writer.Write(value.ToString(CultureInfo.InvariantCulture));
		}

		public void WriteTo(TextWriter writer, double value)
		{
			writer.Write(value.ToString(CultureInfo.InvariantCulture));
		}

		public void WriteTo(TextWriter writer, decimal value)
		{
			writer.Write(value.ToString(CultureInfo.InvariantCulture));
		}

		[Obsolete("未实现", true)]
		public void WriteAttribute(string name, Tuple<string, int> prefix, Tuple<string, int> suffix, params object[] fragments)
		{
			throw new NotSupportedException();
		}

		[Obsolete("未实现", true)]
		public void WriteAttributeTo(TextWriter writer, string name, Tuple<string, int> prefix, Tuple<string, int> suffix, params object[] fragments)
		{
			throw new NotSupportedException();
		}
	}
}
